package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.ann.PermissionName;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    //员工页面列表数据
    @RequestMapping("")
    @PermissionName("员工视图")
    @RequiresPermissions("employee:view")
    public String index() {
        return "employee";
    }


    @RequestMapping("list")
    @ResponseBody
    @PermissionName("员工列表")
    @RequiresPermissions("employee:delete")
    public PageResult list(Model model) {
        return employeeService.list();
    }


    @RequestMapping("listAll")
    @ResponseBody
    public List<Employee> listAll(Model model) {
        return employeeService.listAll();
    }


    @RequestMapping("save")
    @ResponseBody
    @PermissionName("员工新增")
    @RequiresPermissions("employee:delete")
    public JsonResult save(Employee employee) {
        JsonResult jsonResult = new JsonResult();
        try {
            employeeService.save(employee);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonResult;
    }

    @RequestMapping("update")
    @ResponseBody
    @PermissionName("员工保存")
    @RequiresPermissions("employee:delete")
    public JsonResult update(Employee employee) {
        JsonResult jsonResult = new JsonResult();
        try {
            employeeService.update(employee);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonResult;
    }

    //删除员工
    @RequestMapping("delete")
    @ResponseBody
    @PermissionName("员工删除")
    @RequiresPermissions("employee:delete")
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            employeeService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonResult;
    }


}
